
 
cd "`c(pwd)'"   // Set working directory to wherever the user placed "analysis"

* Define global paths relative to the main directory
global main_dir "`c(pwd)'"
global data_dir "$main_dir/data_analysis"
global rawdata_dir "$main_dir/data_raw"
global tables_dir "$main_dir/tables"

* Load data
cd "$data_dir/"

use crsp_compustat_1990_2020_clean, clear
keep gvkey year sic
save gvkey_year_sic, replace
use avgCOWtn4, clear
sort year gvkey
save temp1, replace

cd "$rawdata_dir/"
use master2_data.dta, clear
cd "$data_dir/"

sort year gvkey
merge year gvkey using temp1
drop if _merge==2
drop _merge

egen mpeer1=mean(peerhave1), by(year)
replace peerhave1=peerhave1-mpeer1
egen mpeer3=mean(peerhave3), by(year)
replace peerhave3=peerhave3-mpeer3

bys year: egen COWpct50l1 = pctile(COWtn4) if COWtn4!=. , p(50)
gen highCOWl1=.
replace highCOWl1=0 if COWtn4<COWpct50l1 & COWtn4!=. 
replace highCOWl1=1 if COWtn4>=COWpct50l1 & COWtn4!=. 
replace highCOWl1=0 if COWtn4==0  
label var highCOWl1 "TN4 COW in t-1"
replace COWtn4=highCOWl1

capture drop odvar zzz zzz2 cc cc2 ccc ccc2 oc
g odvar=rodrat4
g zzz=peerhave1*odvar
g zzz2=peerhave3*odvar
g cc=peerhave1*COWtn4
g cc2=peerhave3*COWtn4
g oc=odvar*COWtn4
g ccc=peerhave1*odvar*COWtn4
g ccc2=peerhave3*odvar*COWtn4

tostring  wkey, generate(wkeys) 
tostring year, generate(years)
gen wkeyxyear=wkeys+years

joinby gvkey year using gvkey_year_sic, unmatched(master)
rename _merge _merge_sic
destring sic, replace

* Table 8:

reghdfe bfocal peerhave1 odvar zzz  if bspecific  , absorb(gvkey wkeyxyear) cluster(sic#year gvkey)

reghdfe bfocal peerhave1           COWtn4 cc  if bspecific   , absorb(gvkey wkeyxyear) cluster(sic#year gvkey)

reghdfe bfocal peerhave1 odvar zzz COWtn4 cc  if bspecific   , absorb(gvkey wkeyxyear) cluster(sic#year gvkey)

reghdfe bfocal peerhave1 odvar zzz COWtn4 cc oc ccc  if bspecific   , absorb(gvkey wkeyxyear) cluster(sic#year gvkey)


	













